package cst.a703xmq.startproject_703xmq.Model;

import cst.a703xmq.startproject_703xmq.Bean.WeatherResult;
import cst.a703xmq.startproject_703xmq.Iface.WeatherIface;
import cst.a703xmq.startproject_703xmq.Listener.WeatherListener;
import cst.a703xmq.startproject_703xmq.Service.WeatherService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by bboy knowns on 2017/4/26.
 */

public class WeatherModel implements WeatherIface{
    private WeatherService service;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://www.weather.com.cn/";

    public WeatherModel()
    {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityNumber, final WeatherListener listener)
    {
        service = retrofit.create(WeatherService.class);
        Call<WeatherResult> call = service.getResult(cityNumber);
        call.enqueue(new Callback<WeatherResult>()
        {
            @Override
            public void onResponse(Call<WeatherResult> call,Response<WeatherResult> response)
            {
                listener.onResponse(response.body().getWeatherInfo());
            }
            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t)
            {
                listener.onFail(t.toString());
            }
        });
    }
}